<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/NaturalCustomer.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/LegalClient.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Place.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/ClientAddress.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Phone.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/PurchaseOrder.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/PurchaseOrderDetail.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	/*
	* Command to create a bill
	*/
	class CommandCreatePurchaseOrder extends CustomCommand {
		/*
		* Constructor of the class
		*/
		public function __construct($ciRifList, $ciRif, $clientName, $secondName, 
			$firstLastName, $secondLastName, $emailAddress, $startDate, $endDate, $country, 
			$stateProvince, $city, $zone, $homePhone, $mobilePhone, $workPhone, $faxPhone, 
			$purchaseOrderNumber, $purchaseOrderControlNumber, $purchaseOrderDate, $tax,$readStatus, $products) {
			/**
			 * Creating a client
			 */
			if ($ciRifList == 'ci') {
				$this -> clientType = 'NATURAL_CUSTOMER';
				$this -> naturalCustomer = new NaturalCustomer($ciRif, $clientName, $secondName, 
					$firstLastName, $secondLastName, $emailAddress);
			}
			
			else {
				$this -> clientType = 'LEGAL_CLIENT';
				$this -> legalClient = new LegalClient($ciRif, $clientName, $emailAddress);
			}
			
			/**
			 * Creating a place
			 */
			$this -> country = new Place("PAIS", $country);
			$this -> stateProvince = new Place("ESTADO/PROVINCIA", $stateProvince);
			$this -> city = new Place("CIUDAD", $city);
			$this -> zone = new Place("SECTOR", $zone);
			
			/**
			 * Creating an address
			 */
			$this -> clientAddress = new ClientAddress($startDate, $endDate, $ciRif, $ciRifList);
			
			/**
			 * Creating a home phone
			 */
			$homePhone = explode('.', $homePhone); 
			$homePhoneSerial = $homePhone[0];
			$homePhoneNumber = $homePhone[1];
			$this -> homePhone = new Phone('HABITACION', $homePhoneSerial, $homePhoneNumber, 
				$ciRif, $this -> clientType);
			
			/**
			 * Creating a mobile phone
			 */
			$mobilePhone = explode('.', $mobilePhone); 
			$mobilePhoneSerial = $mobilePhone[0];
			$mobilePhoneNumber = $mobilePhone[1];
			$this -> mobilePhone = new Phone('MOVIL', $mobilePhoneSerial, $mobilePhoneNumber, 
				$ciRif, $this -> clientType);
				
			/**
			 * Creating a work phone
			 */
			$workPhone = explode('.', $workPhone); 
			$workPhoneSerial = $workPhone[0];
			$workPhoneNumber = $workPhone[1];
			$this -> workPhone = new Phone('TRABAJO', $workPhoneSerial, $workPhoneNumber, 
				$ciRif, $this -> clientType);
			
			/**
			 * Creating a fax phone
			 */
			$faxPhone = explode('.', $faxPhone); 
			$faxPhoneSerial = $faxPhone[0];
			$faxPhoneNumber = $faxPhone[1];
			$this -> faxPhone = new Phone('FAX', $faxPhoneSerial, $faxPhoneNumber, 
				$ciRif, $this -> clientType);
			
			/**
			 * Creating a Purchase Order
			 */
			 $GLOBALS['debug'] -> log($purchaseOrder,"Creacion Comando-- Purchase Order");
			$this -> purchaseOrder = new PurchaseOrder($purchaseOrderNumber, $purchaseOrderControlNumber, $purchaseOrderDate, $tax,$readStatus, $ciRif, $ciRifList);
				
			/**
			 * Creating products
			 */
			$this -> products = $products;
		}
		
		/*
		* Method to execute to create a Purchase Order
		*/
		public function execute() {
			if (strcmp($this -> clientType, 'NATURAL_CUSTOMER') == 0) {			
				/**
				 * Creating a natural customer
				 */
				$command = CommandFactory :: createCommandCreateNaturalCustomer
					($this -> naturalCustomer -> getPkIdNumber(), 
					$this -> naturalCustomer -> getFirstName(), 
					$this -> naturalCustomer -> getSecondName(), 
					$this -> naturalCustomer -> getFirstLastName(), 
					$this -> naturalCustomer -> getSecondLastName(), 
					$this -> naturalCustomer -> getEmailAddress());
				$command -> execute();
			}
			
			else {
				/**
				 * Creating a legal client
				 */
				$command = CommandFactory :: createCommandCreateLegalClient
					($this -> legalClient -> getPkRif(), $this -> legalClient -> getName(), 
					$this -> legalClient -> getEmailAddress());
				$command -> execute();
			}
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a country
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> country -> getType(), 
				$this -> country -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a state/province
			 */
			$command = CommandFactory :: createCommandCreatePlace
				($this -> stateProvince -> getType(), $this -> stateProvince -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a city
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> city -> getType(), 
				$this -> city -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a zone
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> zone -> getType(), 
				$this -> zone -> getName());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a client address
			 */
			$command = CommandFactory :: createCommandCreateClientAddress
				($this -> clientAddress -> getStartDate(), 
				$this -> clientAddress -> getEndDate(), $this -> clientAddress -> getIdClient(), 
				$this -> clientAddress -> getCiRif());
			$command -> execute();
			
			/**
			 * Creating a home phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> homePhone -> getType(),
				$this -> homePhone -> getSerial(), $this -> homePhone -> getNumber(), 
				$this -> homePhone -> getIdOwner(), $this -> homePhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a mobile phone
			 */
			$command = CommandFactory :: createCommandCreatePhone
				($this -> mobilePhone -> getType(), $this -> mobilePhone -> getSerial(), 
				$this -> mobilePhone -> getNumber(), $this -> mobilePhone -> getIdOwner(), 
				$this -> mobilePhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a work phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> workPhone -> getType(),
				$this -> workPhone -> getSerial(), $this -> workPhone -> getNumber(), 
				$this -> workPhone -> getIdOwner(), $this -> workPhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a fax phone
			 */
			$command = CommandFactory :: createCommandCreatePhone($this -> faxPhone -> getType(),
				$this -> faxPhone -> getSerial(), $this -> faxPhone -> getNumber(), 
				$this -> faxPhone -> getIdOwner(), $this -> faxPhone -> getTypeOwner());
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a Purchase Order
			 */
			$daoFactory = DaoFactory :: getMySqlDaoFactory();
			$ibad = $daoFactory -> getMySqlPurchaseOrderAccessDao();
						 $GLOBALS['debug'] -> log($this -> purchaseOrder,"Llamando al DAO -- Purchase Order");
			$purchaseOrderCreation = $ibad -> createPurchaseOrder($this -> purchaseOrder);
			
			/**
			 * Creating Purchase Order details
			 */
			  $GLOBALS['debug'] -> log($purchaseOrder,"Creacion Comando-- Purchase Order Detail");
			for ($i = 0; $i < count($this -> products['pk_code']); $i++) {
				$command = CommandFactory :: createCommandCreatePurchaseOrderDetail
					($this -> products['pk_line'][$i], $this -> products['units'][$i], 
					$this -> products['quantity'][$i], $this -> products['unit_cost'][$i], 
					$this -> products['purchase_order_number'][$i], 
					$this -> products['purchase_order_control_number'][$i], 
					$this -> products['pk_code'][$i], $this -> products['branch_rif'][$i]);
				$command -> execute();
			}
	
			$this -> result = $purchaseOrderCreation;
		}
	}
?>